package com.changgou.file.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.file.util.FastDFSClient;
import com.changgou.file.util.FastDFSFile;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/file")
public class FileController {

    @PostMapping("/upload")
    public Result uploadFile(MultipartFile file) {
        try {
            //判断当前的文件是否存在
            if (file == null) {
                throw new RuntimeException("文件不存在");
            }

            //获取文件的完整名称
            String filename = file.getOriginalFilename();
            if (StringUtils.isEmpty(filename)) {
                throw new RuntimeException("文件不存在");
            }

            //获取文件的扩展名称
            String extName = filename.substring(filename.indexOf(".") + 1);

            //获取文件的内容
            byte[] content = file.getBytes();

            //创建文件上传的实体封装类
            FastDFSFile fastDFSFile = new FastDFSFile(filename, content, extName);


            //基于工具类进行文件上传，并接受返回参数  String[]
            String[] uploadResult = FastDFSClient.upload(fastDFSFile);


            //封装返回结果
            String url = FastDFSClient.getTrackerUrl() + uploadResult[0] + "/" + uploadResult[1];
            return new Result(true, StatusCode.OK, "文件上传成功", url);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new Result(false, StatusCode.ERROR, "上传文件失败");
    }
}
